package cn.hejiang.org.controller;

import cn.hejiang.basic.util.AjaxResult;
import cn.hejiang.basic.util.PageList;
import cn.hejiang.org.domain.DataDictionary;
import cn.hejiang.org.query.DataDictionaryQuery;
import cn.hejiang.org.service.IDataDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dic")
@Api(tags = "数据字典接口", description = "数据字典接口详细描述")
public class DataDictionaryController {
    @Autowired
    private IDataDictionaryService dataDictionaryService;

    // 新增或修改
    @PutMapping
    @ApiOperation(value = "添加或修改", notes = "带Id为添加，没有就是新增")
    public AjaxResult addOrUpdate(@RequestBody DataDictionary dataDictionary) {
        try {
            // 判断id是为空
            if (dataDictionary.getId() == null) {
                dataDictionaryService.add(dataDictionary); // 如果id为空则调用add方法
            } else {
                dataDictionaryService.update(dataDictionary);// 如果id不为空则调用update方法
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常，以滚出"); // 操作失败
        }

    }

    // 删除员工
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        try {
            dataDictionaryService.delete(id); // 调用删除方法
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常，以滚出"); // 操作失败
        }
    }

    // 查询一个
    @GetMapping("/{id}")
    public DataDictionary queryById(@PathVariable Long id) {
        return dataDictionaryService.queryById(id);
    }

    // 查询所有
    @GetMapping
    public List<DataDictionary> queryAll() {
        return dataDictionaryService.queryAll();
    }

    // 分页 + 高级查询
    @PostMapping
    public PageList<DataDictionary> queryPage(@RequestBody DataDictionaryQuery query) {
        return dataDictionaryService.queryPage(query);
    }

    // 批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            dataDictionaryService.patchDelete(ids); // 调用删除方法
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常，以滚出"); // 操作失败
        }
    }
}
